$(function() {
	let newsId = $("._newsId").text();
	let page = 1;
	let userName = $(".userName").text();
	/**
	 * 加载分页数据
	 * @param {Object} current
	 */
	let loadData = function(current) {
		$.get(
			"newsComments/news-comments-json.do",
			"commentCommentator=" + newsId + "&current=" + current + "&rowMax=5",
			function(data) {
				formatData(data.list);
				$(".contentTotal").text(data.total + "条评论");
				formatPage(data);
			}, "json"
		);
	}
	let dataCommentDiv = $("._comment").remove();
	/**
	 * 格式化数据
	 * @param {Object} list
	 */
	let formatData = function(list) {
		$.each(list, function(i, item) {
			let div = dataCommentDiv.clone();
			$("._nextPage").before(div);
			$.each(item, function(name, value) {
				div.find("._" + name).text(value);
			});
			div.find(".jubao").attr("data-id", item.commentId);
			div.find("._deleteComment").attr("data-id", item.commentId);
			
			$.get(
				"newsComments/comment-realName.do",
				"commentId="+item.commentId,
				function(data){
					fn1(data);
				},"json"
			);
			let fn1=function(data){
				div.find("._commentRealName").text(data.userRealName);
				if(data.userImg!=null&data.userImg!=""){
					div.find(".userImg").attr("src",data.userImg);
				}	
			}
			if(userName == item.commentUserName) {
				div.find("._deleteComment").attr("style", "color: red; display: block; text-decoration: none;");
			}
		});

	}
	/**
	 * 格式化分页按钮
	 * @param {Object} p
	 */
	let formatPage = function(p) {
		if(parseInt(p.total) > p.pageSize * page) {
			$("._nextPage").attr("style", "display:block");
			$("._null").attr("style", "display:none");
		} else {
			$("._nextPage").attr("style", "display:none");
			$("._null").attr("style", "display:block");
		}
	}
	loadData(page);
	/**
	 * 处理分页事件
	 */
	$("._nextPage").on("click", function() {
		page++;
		loadData(page);
	});
	/**
	 * 表单的提交事件
	 */
	let doCommentAdd = function() {
		if($(".ping").text() == '登录') {
			window.location = 'http://127.0.0.1:8080/login.html';
		} else if($(".ping").text() == '评论') {
			let form = $(this);
			$.post(
				"newsComments/news-comments-add.do",
				form.serializeArray(),
				function(data) {
					if(data.ok) {
						toastr.success("发表成功");
						form.trigger("reset");
						$("._comment").remove();
						for(let i = 1; i <= page; i++) {
							loadData(i);
						}
					} else {
						toastr.error(data.message);
					}
				}, "json"
			);
		}

		return false;
	}
	let comments = $(".comments");
	comments.find("form").on("submit", doCommentAdd);

	let deleteDialog; //删除确认对话框
	let deleteBtn; //删除按钮

	//加载删除确认对话框
	$.get(
		"comments/news-comments-delete.html",
		function(html) {
			deleteDialog = $(html).filter(".modal").appendTo("body");

			//处理删除按钮事件
			deleteDialog.find("._confirm").on("click", function() {
				deleteDialog.modal("hide");
				$.post(
					"newsComments/news-comments-delete.do", {
						"commentId": deleteBtn.data("id")
					},
					function(data) {
						if(data.ok) {
							toastr.success("删除成功");
							$("._comment").remove();
							for(let i = 1; i <= page; i++) {
								loadData(i);
							}
						} else {
							toastr.error("删除失败：" + data.message);
						}
					}, "json"
				);
				return false;
			});

		}
	);

	//绑定删除按钮的单击事件
	$("body").on("click", "._deleteComment", function() {
		deleteBtn = $(this);
		//		console.info(deleteDialog);
		deleteDialog.modal("show");
		return false;
	});
})
